package com.it.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionUtils {
    public static final String DRIVER = "com.mysql.jdbc.Driver";
    public static final String URL = "jdbc:mysql://localhost:3306/qqzonedb?useUnicode=true&characterEncoding=utf-8&useSSL=false";
    public static final String USER = "root";
    public static final String PWD = "root";
    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();

    /**
     * 获取链接
     *
     * @return
     */
    public static Connection getConection() {
        if (threadLocal.get() == null) {
            threadLocal.set(creatConection());
        }
        return threadLocal.get();

    }

    /**
     * 创建链接
     *
     * @return
     */
    private static Connection creatConection() {
        try {
            //1.加载驱动
            Class.forName(DRIVER);
            //2.通过驱动管理器获取连接对象
            return DriverManager.getConnection(URL, USER, PWD);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
